<template>
  <div class="menu-bar-container">
    <div class="logo"
         :style="{'background':themeColor}"
         :class="collapse?'menu-bar-collapse-width':'menu-bar-width'"
         @click="$router.push('/')">
      <img v-if="collapse" src="@/assets/logo.png"/>
      <div>{{collapse?'':appName}}</div>
    </div>
    <!--导航菜单-->
    <el-menu ref="navMenu"
             default-active="1"
             :class="collapse ? 'menu-bar-collapse-width':'menu-bar-width'"
             :collapse="collapse"
             :collapse-transition="false"
             :unique-opened="true"
             @open="handleOpen"
             @close="handleClose"
             @select="handleSelect">
      <!--导航菜单树组件，动态加载菜单-->
      <menu-tree v-for="item in navTree" :key="item.id" :menu="item"></menu-tree>
    </el-menu>
  </div>
</template>

<script>
  import {mapState} from 'vuex'
  import MenuTree from "../components/MenuTree"

  /*左侧导航区域*/
  export default {
    name: "NavBar",
    components: {
      MenuTree
    },
    computed: {
      ...mapState({
        appName: state => state.app.appName,
        themeColor: state => state.app.themeColor,
        collapse: state => state.app.collapse,
        navTree: state => state.menu.navTree
      }),
      mainTabs: {
        get() {
          return this.$store.state.tab.mainTabs
        },
        set(val) {
          this.$store.commit('updateMainTabs', val)
        }
      },
      mainTabsActiveName: {
        get() {
          return this.$store.state.tab.mainTabsActiveName
        },
        set(val) {
          this.$store.commit('updateMainTabsActiveName', val)
        }
      }
    },
    watch: {
      $route: 'handleRoute'
    },
    methods: {
      //路由操作出来
      handleRoute(route) {
        //tab标签选中，如果不存在则添加
        let tab = this.mainTabs.filter(it => it.name === route.name)[0];
        if (!tab) {
          tab = {
            name: route.name,
            title: route.title,
            icon: route.meta.icon
          };
          this.mainTabs = this.mainTabs.concat(tab);
        }
        this.mainTabsActiveName = tab.name;
        //切换标签页时同步更新高亮菜单
        if (this.$refs.navMenu !== null) {
          this.$refs.navMenu.activeIndex = '' + route.meta.index;
          this.$refs.navMenu.initOpenedMenu();
        }
      },
      handleOpen() {
        console.log('handleOpen');
      },
      handleClose() {
        console.log('handleClose');
      },
      handleSelect() {
        console.log('handleSelect');
      }
    }
  }
</script>

<style scoped>
  .menu-bar-container {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 1020;
  }

  .menu-bar-container .el-menu {
    position: absolute;
    top: 60px;
    bottom: 0;
    text-align: left;
  }

  .menu-bar-container .logo {
    position: absolute;
    top: 0;
    height: 60px;
    line-height: 60px;
    background: #545c64;
    cursor: pointer;
  }

  .menu-bar-container .logo img {
    width: 40px;
    height: 40px;
    border-radius: 0;
    margin: 10px 10px 10px 10px;
    float: left;
  }

  .menu-bar-container .logo div {
    font-size: 22px;
    color: white;
    text-align: left;
    padding-left: 20px;
  }

  .menu-bar-container .menu-bar-width {
    width: 200px;
  }

  .menu-bar-container .menu-bar-collapse-width {
    width: 65px;
  }
</style>
